var dateUtils = require('../../utils/dateUtil.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    switchList:[
      {title:'进行中',data:{currPage: 1,list: [],pageSize: 10,totalCount: 0,totalPage: 0},beginTime:'2022-05-23 00:00:00',endTime:'2022-06-23 23:59:59',type:0},
      {title:'已结束',data:{currPage: 1,list: [],pageSize: 10,totalCount: 0,totalPage: 0},beginTime:'2022-05-23 00:00:00',endTime:'2022-06-23 23:59:59',type:1},
      {title:'已发布',data:{currPage: 1,list: [],pageSize: 10,totalCount: 0,totalPage: 0},beginTime:'2022-05-23 00:00:00',endTime:'2022-06-23 23:59:59',type:2}
    ],

    selectIndex:0,
    beginTime:'',
    endTime:'',

    totalPage:0,
    pageNo:1,
    pageSize:10,
    isLoading:false,
    allloaded: false,

    isRefresh: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "体育作业"
    })
    this.showDate();
    this.getData();
  },

  getData(cd){
    var vt = this.data.switchList[this.data.selectIndex]
    var data = {
      beginTime:vt.beginTime,
      endTime:vt.endTime,
      type:vt.type,
      pageNo:this.data.pageNo,
      pageSize:this.data.pageSize,
    }
    console.log("作业列表参数="+JSON.stringify(data));
    wx.showLoading({
      title: '数据加载中...',
      icon: 'none',
    })
    this.setData({
      isLoading:true
    })
    var that = this;
    app.api.homeworkList(data).then(rest => {
      wx.hideLoading()
      this.setData({
        isLoading:false
      })
      cd && cd()
      if(rest&&rest.success){   
        var vst  =  that.data.switchList;
        var dataList = [...vst[that.data.selectIndex].data.list,...rest.data.list]
        vst[that.data.selectIndex].data =rest.data
        vst[that.data.selectIndex].data.list = dataList
        
        that.setData({
          switchList:vst,
          totalPage:rest.data.totalCount
        })
        // console.log('列表个数数据='+this.data.switchList[that.data.selectIndex].data.list.length)
        //console.log('列表数据='+JSON.stringify(this.data.switchList))
      }else{
        wx.showToast({
          title: rest.errMsg||'网络异常,请重试！',
          icon: 'none',
          duration: 2000,
          mask:true
        })
      }
    })
    .catch(r => {
      console.error(r)
      wx.showToast({
        title: '登录失败,请重试！',
        icon: 'none',
        duration: 2000,
        mask:true
      })
    })
  },

  showDate(){
    var starTiem = dateUtils.mathChangeDate(dateUtils.formatTime(new Date()),"-",7,true,'')
    var dateCom =  this.selectComponent("#date-select");
    dateCom.onShowAlert(starTiem+" 00:00:00",null,['发布作业']);

    var endTime = dateCom.data.endTimeStr 
    var beginTime = dateCom.data.startTimeStr ;
    var list = this.data.switchList;
    for(var i=0;i<list.length;i++){
      var item = list[i];
      item.beginTime = beginTime
      item.endTime  = endTime
    }
    this.setData({
      switchList:list,
      beginTime:beginTime,
      endTime:endTime
    })

    // console.log("时间 switchList=="+this.data.beginTime+"==="+this.data.endTime+"==="+JSON.stringify(this.data.switchList));
  },
  
  //tab页面切换
  didClickIndex(e){
   
    let index = e.detail;
    var listData = this.data.switchList;
    listData[index].data.list = []
    console.log("=====didClickIndex="+index);
    this.setData({
        selectIndex:index,
        pageNo:1,
        switchList:listData,
        totalPage:0
    })
    this.getData()
  },

  //发布作业
  titleDateIndex(data){
    
    wx.navigateTo({
      url: '/pages/record/applyWorks?isEdit=1',
      
    })
  },
 
  //结束时间按钮确认
  bindTimeChangeEnd(val){
    let repTime = val.detail.replace(/-/g, '/');
    let timeTamp = Date.parse(repTime); 
    let starTime = Date.parse(this.data.beginTime); 
    console.log(timeTamp+' Endpicker发送选择改变，携带值为'+ val.detail+"==="+starTime)
    if(timeTamp>=starTime){
      var list = this.data.switchList;
      for(var i=0;i<list.length;i++){
        var item = list[i];
        item.endTime = val.detail
      }
     
      list[this.data.selectIndex].data.list = []

      this.setData({
        switchList:list,
        endTime:val.detail,
        pageNo:1,
       
        totalPage:0
      })
      console.log("End确认时间End==="+JSON.stringify(this.data.switchList))
      this.getData()
    }else{
      console.log("时间 switchList=="+this.data.beginTime+"==="+this.data.endTime);
      var dateCom =  this.selectComponent("#date-select");
      dateCom.onShowAlert(this.data.beginTime,this.data.endTime,['发布作业']);
      wx.showToast({
        title: '结束时间需大于开始时间',
        icon: 'none',
        duration: 1500,
        mask:true
      })
    }
  },

  //开始时间按钮确认
  bindTimeChangeStart(val){
    let repTime = val.detail.replace(/-/g, '/');
    let timeTamp = Date.parse(repTime); 
    let endTime = Date.parse(this.data.endTime); 
    console.log(timeTamp+' Startpicker发送选择改变，携带值为'+ val.detail+"==="+endTime)
    if(timeTamp<=endTime){
      var list = this.data.switchList;
      for(var i=0;i<list.length;i++){
        var item = list[i];
        item.beginTime = val.detail
      }
      list[this.data.selectIndex].data.list = []
      this.setData({
        switchList:list,
        beginTime:val.detail,
        pageNo:1,
       
        totalPage:0
      })
      //console.log("Start确认时间Start==="+JSON.stringify(this.data.switchList))
      this.getData()
    }else{
      //console.log("时间 switchList=="+this.data.beginTime+"==="+this.data.endTime);
      var dateCom =  this.selectComponent("#date-select");
      dateCom.onShowAlert(this.data.beginTime,this.data.endTime,['发布作业']);
      wx.showToast({
        title: '开始时间需小于结束时间',
        icon: 'none',
        duration: 1500,
        mask:true
      })

    }
  },


  //作业详情
  goDetail(e){
   
   
    var itemData = JSON.stringify(this.data.switchList[this.data.selectIndex].data.list[e.currentTarget.dataset.index]);
    //console.log("作业详情4==="+itemData);
    //已发布
    if(this.data.selectIndex == 2){
      wx.navigateTo({
         url: '../../pages/record/editTeacherWorkDetai?info='+JSON.stringify(this.data.switchList[this.data.selectIndex].data.list[e.currentTarget.dataset.index]),
      })
    }else{
      //进行中、已结束
      wx.navigateTo({
        url: '../../pages/record/detail?info='+JSON.stringify(this.data.switchList[this.data.selectIndex].data.list[e.currentTarget.dataset.index]),
      })
    }
    //console.log(this.data.selectIndex+"作业详情3==="+e.currentTarget.dataset.index);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.isRefresh == 1){
      console.log("列表个数数据 刷新数据");
      var listData = this.data.switchList;
      listData[0].data.list = [];
      listData[1].data.list = [];
      listData[2].data.list = [];
      
      this.setData({
          pageNo:1,
          switchList:listData,
          totalPage:0
      })

      this.showDate();
      this.getData();
    }
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  // 加载更多
  loadmore({
    detail
  }) {
    
    console.log('加载更多1='+this.data.pageSize * this.data.pageNo)
    //TODO 修改

    var dataList  =  this.data.switchList;

   
   var data = dataList[this.data.selectIndex].data;
   var totalCount = data.totalCount


    if(data.currPage*data.pageSize >= totalCount){
  

      data.currPage =totalCount
     
        wx.showToast({
          title: '暂无更多记录！',
          icon: 'none',
        })
      this.setData({
        isLoading:false
      })

      detail.success();

    
      return
    }
 

    if(this.data.isLoading){
      return
    }

    this.setData({
      pageNo:this.data.pageNo + 1
    })
    this.getData(()=>{
      detail.success();
    })
  },
  // 刷新
  refresh({
    detail
  }) {
    console.log('下拉刷新1='+this.data.pageSize * this.data.pageNo)

    var dataList  =  this.data.switchList;

   
    dataList[this.data.selectIndex].data.list = []

    this.setData({
      pageNo:1,
      switchList:dataList,
      totalPage:0,
      loading: false,
      allloaded: false,
    })
    this.getData(()=>{
      detail.success();
    })
  },




  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})